package com.realtyManage.springboot.Dao;


import com.realtyManage.springboot.Model.Do.Commodity;
import com.realtyManage.springboot.Model.Do.Viki;
import io.lettuce.core.dynamic.annotation.Param;
import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.data.jpa.repository.JpaSpecificationExecutor;
import org.springframework.data.jpa.repository.Modifying;
import org.springframework.data.jpa.repository.Query;
import org.springframework.stereotype.Repository;

import javax.transaction.Transactional;
import java.util.List;

/**
 * 积分商品
 *
 * @author 杭州素色网络科技有限公司
 * @since 2019-12-13
 */

@Repository
public interface CommodityRepository extends JpaRepository<Commodity, Long>, JpaSpecificationExecutor<Commodity> {

    @Modifying
    @Transactional
    @Query("update Commodity a set " +
            "a.commodityName =  CASE WHEN :#{#commodity.commodityName} IS NULL THEN a.commodityName ELSE :#{#commodity.commodityName} END, " +
            "a.integral =  CASE WHEN :#{#commodity.integral} IS NULL THEN a.integral ELSE :#{#commodity.integral} END, " +
            "a.num =  CASE WHEN :#{#commodity.num} IS NULL THEN a.num ELSE :#{#commodity.num} END, " +
            "a.address =  CASE WHEN :#{#commodity.address} IS NULL THEN a.address ELSE :#{#commodity.address} END, " +
            "a.detail =  CASE WHEN :#{#commodity.detail} IS NULL THEN a.detail ELSE :#{#commodity.detail} END, " +
            "a.failureTime =  CASE WHEN :#{#commodity.failureTime} IS NULL THEN a.failureTime ELSE :#{#commodity.failureTime} END, " +
            "a.stock =  CASE WHEN :#{#commodity.stock} IS NULL THEN a.stock ELSE :#{#commodity.stock} END, " +
            "a.state =  CASE WHEN :#{#commodity.state} IS NULL THEN a.state ELSE :#{#commodity.state} END, " +
            "a.commodityImg =  CASE WHEN :#{#commodity.commodityImg} IS NULL THEN a.commodityImg ELSE :#{#commodity.commodityImg} END, " +
           /* "a.commodityCode =  CASE WHEN :#{#commodity.commodityCode} IS NULL THEN a.commodityCode ELSE :#{#commodity.commodityCode} END, " +
            "a.commodityCodeUrl =  CASE WHEN :#{#commodity.commodityCodeUrl} IS NULL THEN a.commodityCodeUrl ELSE :#{#commodity.commodityCodeUrl} END, " +*/
            "a.price =  CASE WHEN :#{#commodity.price} IS NULL THEN a.price ELSE :#{#commodity.price} END " +
            "where a.id = :#{#commodity.id}")
    int updateCommodity(@Param("commodity") Commodity commodity);

    @Modifying
    @Transactional
    @Query(value = "delete from commodity where  id in (?1)",nativeQuery = true)
    int deleteCommodity(List id);

    @Query(value = "select * from commodity where  id = ?1",nativeQuery = true)
    Commodity findCommodityById(String id);

    @Query(value = "select stock from commodity where  id = ?1",nativeQuery = true)
    int findNumById(Integer id);

    @Query(value = "select num from commodity where  id = ?1",nativeQuery = true)
    int findNum(Integer id);


}
